//
//  HMNetWorkTools.swift
//  WeiBoXR
//
//  Created by acer on 16/9/22.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit
import AFNetworking

enum requestType {
    case GET
    case POST
}
class HMNetworkTools: AFHTTPSessionManager {
    
    static let sharedTools: HMNetworkTools = {
        
        let tools:HMNetworkTools = HMNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    fileprivate func request(requestType:requestType , URLString: String , param: Any? ,callBack: @escaping (Any? , Error?) -> ()) {
        
        if requestType == .GET {
            get(URLString, parameters: param, progress: nil, success: { (_, response) in
                callBack(response,nil)
                }, failure: { (_, error) in
                    callBack(nil,error)
            })
        }
        else{
            post(URLString, parameters: param, progress: nil, success: { (_, response) in
                callBack(response,nil)
                }, failure: { (nil, error) in
                    callBack(nil,error)
            })
        }
    }
    
    
}


extension HMNetworkTools {
    
    func requestAccessToken(code: String , callBack: @escaping (Any? , Error?) -> ())  {
        
        let url = "https://api.weibo.com/oauth2/access_token"
        let param = [
            "client_id": weiboAPPKey,
            "client_secret": weiboClient_secret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": weiboRedirect_Uri
        ]
        
        request(requestType: .POST, URLString: url, param: param, callBack: callBack)
    }
    //  根据code获取accesstoken
    func requestUserInfo(accessToken: String, uid: Int64, callBack: @escaping (Any?, Error?) -> ()) -> Void {
        //  请求接口地址
        let url = "https://api.weibo.com/2/users/show.json"
        //  准备请求需要的参数
        let params: [String: Any] = [
            "access_token": accessToken,
            "uid": uid
        ]
        
        request(requestType: .GET, URLString: url, param: params, callBack: callBack)
        
    }
    
    //  MARK: -- 微博首页相关接口
    func requestStatus(accessToken: String, maxId: Int64, sinceId: Int64,callBack: @escaping (Any? ,Error?) -> ()) {
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params: [String: Any] = [
            "access_token": accessToken,
            "max_id": maxId,
            "since_id": sinceId

        ]
        print("================================")
        print(url+"?access_token=\(accessToken)")
        request(requestType: .GET, URLString: url, param: params, callBack: callBack)
    }
    
    //  发微博相关接口
    func update(accessToken: String, status: String, callBack: @escaping (Any?, Error?)->()) -> Void {
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params: [String: Any] = [
            "access_token": accessToken,
            "status": status
        ]
        request(requestType: .POST, URLString: url, param: params, callBack: callBack)
    }
    //上传图片的接口
    func upload(access_token: String, status: String, image: UIImage, callBack: @escaping (Any?, Error?)->()) -> Void {
        //  准备url地址
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        //  准备参数
        let params = [
            "access_token": access_token,
            "status": status
        ]
        //let imageData = UIImagePNGRepresentation(image)!
        //  compressionQuality 质量系数0-1, 质量系数越大表示图片越清晰
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        post(url, parameters: params, constructingBodyWith: { (formData) in
            formData.appendPart(withFileData: imageData, name: "pic", fileName: "test1", mimeType: "image/jpeg")
            }, progress: nil, success: { (response, nil) in
                callBack(response, nil)
            }) { (nil, error) in
                callBack(nil,error)
        }
    }
}
